import re
from datetime import datetime

from pydantic import BaseModel, Field, validator


class Token(BaseModel):
    access_token: str
    token_type: str


class UserBase(BaseModel):
    username: str = Field(..., min_length=3, max_length=30, description="用户名")
    state: bool = Field(True, description="账户状态")

    @validator('username')
    def validate_username(cls, v):
        if not re.match(r"^[A-Z][a-zA-Z_@]+", v):
            raise ValueError('用户名格式不符合要求')
        return v


class UserCreate(UserBase):
    password: str = Field(..., min_length=8, max_length=30)

    @validator('password')
    def validate_password(cls, v):
        if not re.match(r"^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@_!#$%^&*])[A-Za-z\d@_!#$%^&*]{8,}$", v):
            raise ValueError('密码必须是数字和字母的组合，且必须含有特殊字符')
        return v


class User(UserBase):
    id: int
    state: bool

    class Config:
        orm_model = True


class UserUpdate(BaseModel):
    password: str
